package com.movie.modules.jf.service.impl;

import com.movie.common.utils.NumberUtils;
import com.movie.modules.jf.dao.ActorDao;
import com.movie.modules.jf.dao.MovieCommentDao;
import com.movie.modules.jf.dao.MovieDao;
import com.movie.modules.jf.entity.*;
import com.movie.modules.jf.service.ImageResourceService;
import com.movie.modules.jf.service.MovieCommentService;
import com.movie.modules.jf.service.RelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.movie.common.utils.PageUtils;
import com.movie.common.utils.Query;

import com.movie.modules.jf.dao.VipDao;
import com.movie.modules.jf.service.VipService;
import org.springframework.transaction.annotation.Transactional;


@Service("vipService")
@Transactional
public class VipServiceImpl extends ServiceImpl<VipDao, VipEntity> implements VipService {

    @Autowired
    private MovieCommentService commentService;
    @Autowired
    private RelationService relationService;
    @Autowired
    private ImageResourceService imageResourceService;


    @Autowired
    private MovieDao movieDao;
    @Autowired
    private ActorDao actorDao;
    @Autowired
    private MovieCommentDao commentDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String phone = params.get("phone")==null?null:params.get("phone").toString();
        String nickname= params.get("nickname")==null?null:params.get("nickname").toString();
        Integer status = NumberUtils.objToInteger(params.get("status"));
        Integer sex = NumberUtils.objToInteger(params.get("sex"));
        IPage<VipEntity> page = this.page(
                new Query<VipEntity>().getPage(params),
                new QueryWrapper<VipEntity>().eq(StringUtils.isNotEmpty(phone),"phone",phone).like(StringUtils.isNotEmpty(nickname),"nickname",nickname).eq(status!=null,"status",status).eq(sex!=null,"sex",sex).orderByDesc("vip_id")
        );

        return new PageUtils(page);
    }

    @Override
    public void delete(Integer id) {
        if(this.removeById(id)){
            commentService.remove(new QueryWrapper<MovieCommentEntity>().eq("vip_id",id));
            imageResourceService.remove(new QueryWrapper<ImageResourceEntity>().eq("resource_id",id).eq("resource_type",2));
            relationService.remove(new QueryWrapper<RelationEntity>().eq("relation_id",id).eq("relation_type",4).or().eq("relation_type",5));
        }
    }

    @Override
    public PageUtils getMovieList(Integer vipId,Map<String,Object> params) {
        IPage<MovieEntity> page=movieDao.getMovieByVipId(new Query<MovieEntity>().getPage(params),vipId);
        return new PageUtils(page);
    }

    @Override
    public PageUtils getActorList(Integer vipId,Map<String,Object> params) {
        IPage<ActorEntity> page= actorDao.getActorByVipId(new Query<ActorEntity>().getPage(params),vipId);
        return new PageUtils(page);
    }

    @Override
    public PageUtils getCommentList(Integer vipId, Map<String, Object> params) {
        IPage<MovieCommentEntity> page=new Query<MovieCommentEntity>().getPage(params);
        return new PageUtils(commentDao.getCommentByVipId(page,vipId));
    }


}
